import React, { useEffect } from 'react';
import { Drawer } from 'antd';
import { ProForm, ProFormText } from '@ant-design/pro-components';

export type CustomDrawerProps = {
  open: boolean;
  onClose: () => void;
  onSubmit: (values: any) => Promise<void>;
  type?: 'add' | 'edit'; // 用于区分新增和编辑
  initialValues?: Record<string, any>;
  loading?: boolean;
  children?: React.ReactNode; // 添加children类型声明
};

const CustomDrawer: React.FC<CustomDrawerProps> = ({
  open,
  onClose,
  onSubmit,
  type = 'add',
  initialValues,
  loading = false,
  children,
}) => {
  const [form] = ProForm.useForm();

  useEffect(() => {
    if (open) {
      form.setFieldsValue(initialValues || {});
    } else {
      form.resetFields();
    }
  }, [open, initialValues, form]);

  return (
    <Drawer
      title={type === 'add' ? '新增' : '编辑'}
      open={open}
      onClose={onClose}
      width={500}
      destroyOnClose
    >
      {children}
    </Drawer>
  );
};

export default CustomDrawer;
